<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MoneyOpsclass
 *
 * @author marius
 * Edited by Jamal dine: 23/01/2012
 */

require_once ("Money.class.php");
require_once ("MoneyFactory.class.php");
require_once ("MoneyExceptions.php");

class MoneyOps {

	private static $moneyFactory;

	
	public function __construct($mf){
        MoneyOps::$moneyFactory =$mf;
    }
	
	public static function getMoneyFactory() {
		return MoneyOps::$moneyFactory==null?MoneyFactory::getDefaultFactory():MoneyOps::$moneyFactory;
	}
	
    public static function add($m1,$m2) {
	if (!$m1->sameCurrency($m2)) {
		throw new IncompatiblesCurrenciesException("MoneyOps.add");
	}
        $_factory= MoneyOps::getMoneyFactory();
    	return $_factory->createMoney(
		$m1->getValue()+$m2->getValue(),
		$m1->getCurrency());	
    }
	
	
	public static function sub($m1,$m2) {
	if (!$m1->sameCurrency($m2)) {
		throw new IncompatiblesCurrenciesException("MoneyOps.sub");
	}
        $_factory= MoneyOps::getMoneyFactory();
    	return $_factory->createMoney(
		$m2->getValue()-$m1->getValue(),
		$m1->getCurrency());	
    }
	
	public static function mul($m1,$m2) {
	if (!$m1->sameCurrency($m2)) {
		throw new IncompatiblesCurrenciesException("MoneyOps.mul");
	}
        $_factory= MoneyOps::getMoneyFactory();
    	return $_factory->createMoney(
		$m1->getValue()*$m2->getValue(),
		$m1->getCurrency());	
    }
	
	public static function div($m1,$m2) {
	if (!$m1->sameCurrency($m2)) {
		throw new IncompatiblesCurrenciesException("MoneyOps.div");
	}
        $_factory= MoneyOps::getMoneyFactory();
    	return $_factory->createMoney(
		$m2->getValue()/$m1->getValue(),
		$m1->getCurrency());	
    }
	
	
	public static function convert($m1,$taux) {
	if (!is_numeric($taux)) { /*Verification que $taux est un chiffre*/
		throw new InvalidTauxException("MoneyOps.convert");
	}
        $_factory= MoneyOps::getMoneyFactory();
		$value = (int) (($m1->getValue())*$taux);
    	return $value;	
    }
	
}
?>
